﻿/*----------------------------------------------------------------
// Copyright (C) 2014 苏州麦迪斯顿医疗科技股份有限公司
// 文件名：FaultExceptionHandler.cs
// 文件功能描述：FaultExceptionHandler
// 创建标识：顾伟伟-2014-04-03
----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel.Dispatcher;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace CPOE.Framework.ExceptionHanding
{
    /// <summary>
    /// 自定义的WCF服务端异常处理类
    /// </summary>
   public class FaultExceptionHandler:IErrorHandler
    {

        public bool HandleError(Exception error)
        {
           return  false;
        }

        public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
        {
           
            if (typeof(FaultException).IsInstanceOfType(error))
            {
                return;
            }

            var messageFault = MessageFault.CreateFault(
                FaultCode.CreateReceiverFaultCode("FaultException", "http://medicalsystem.com.cn/Fault"),
                new FaultReason(GetExceptionMessage(error)),
                new ExceptionDetail(error));

            fault = Message.CreateMessage(version, messageFault, "http://medicalsystem.com.cn/Fault/Action");
            
        }

        private string  GetExceptionMessage(Exception ex)
        {
            if (ex.InnerException == null)
                return ex.Message;

            return ex.Message +Environment.NewLine+ ex.InnerException.Message;
        }
       
    }
}
